/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: 陈韬
 * @Date: 2020-04-24 19:20:36
 * @LastEditors: 陈韬
 * @LastEditTime: 2020-04-25 09:40:18
 */
const fs = require('fs');
const { join, extname } = require('path');

/**
 * 拷贝目录
 * @param {string} source 源目录
 * @param {string} target 目标目录
 */
const copyDir = (source, target) => {
    //1. 判断源目录和目标目录是否存在
    if (!fs.existsSync(source)) throw new Error('源目录不存在');
    if (fs.existsSync(target)) throw Error('目标目录已经存在'); //return
    //2. 创建目录
    fs.mkdirSync(target);
    //3. 读取源目录中所有的文件
    fs.readdirSync(source).forEach((item) => {
        //4.判断是不是文件
        if (fs.statSync(join(source, item)).isFile()) { //是文件拷贝
            //5.判断是不是css文件
            if (extname(join(source, item)) === '.css') {
                //读取文件内容替换
                let con = fs.readFileSync(join(source, item), 'utf-8').replace(/background/g, 'color');
                //写入
                fs.writeFileSync(join(target, item), con);
            } else {
                fs.copyFileSync(join(source, item), join(target, item));
            }
        } else { //是目录递归调用
            copyDir(join(source, item), join(target, item));
        }
    })
}

copyDir('public', 'app');